import cv2 as cv
import numpy as np
import numpy as ny


def lines_detect():
    src_img = cv.imread('..//image//road.png')
    src_img = cv.blur(src_img, (3, 3))
    cv.imshow('source image', src_img)
    gray_img = cv.cvtColor(src_img, cv.COLOR_BGR2GRAY)
    cv.imshow('gray image', gray_img)
    edges_img = cv.Canny(gray_img, 95, 185)
    cv.imshow('canny image', edges_img)
    lines = cv.HoughLines(edges_img, 1, np.pi/180, 150)

    for line in lines:
        r, theta = line[0]
        cos_theta = np.cos(theta)
        sin_theta = np.sin(theta)
        x0 = r * cos_theta
        y0 = r * sin_theta

        x1 = int(x0 + 1200 * (-sin_theta))
        y1 = int(y0 + 1200 * cos_theta)
        x2 = int(x0 - 1200 * (-sin_theta))
        y2 = int(y0 - 1200 * cos_theta)

        cv.line(src_img, (x1, y1), (x2, y2), (0, 255, 0), 2)
        print('line:', line)
    cv.imshow('out image', src_img)
    cv.waitKey(0)


def lines_detect_hough_p():
    src_img = cv.imread('../image//road.png')
    gray_img = cv.cvtColor(src_img,cv.COLOR_RGB2GRAY)
    canny_img = cv.Canny(gray_img, 100, 200)
    lines = cv.HoughLinesP(canny_img, 1, np.pi/180, 100, minLineLength=200, maxLineGap=10)
    for line in lines:
        print('line:', line[0])
        x1, y1, x2, y2 = line[0]
        cv.line(src_img, (x1, y1), (x2, y2), (0, 250, 0), 3)
    cv.imshow('lines image', src_img)
    cv.waitKey(0)


def main():
    lines_detect_hough_p()


if __name__ == '__main__':
    main()
